﻿namespace Digitalmes.Core.Common.SerialPorts;

public static class ISerialPortsSwitchExtensions
{
    /// <summary>
    /// 尝试打开默认串口。
    /// </summary>
    /// <param name="serialPortSwitch"></param>
    /// <returns></returns>
    public static bool TryOpen(this ISerialPortsSwitch serialPortSwitch)
    {
        var (ok, _, _) = serialPortSwitch.Open();
        return ok;
    }

    /// <summary>
    /// 尝试打开指定串口。
    /// </summary>
    /// <param name="serialPortSwitch"></param>
    /// <param name="portName">要打开的串口</param>
    /// <returns></returns>
    public static bool TryOpen(this ISerialPortsSwitch serialPortSwitch, string portName)
    {
        var (ok, _) = serialPortSwitch.Open(portName);
        return ok;
    }

    /// <summary>
    /// 尝试打开指定用途的串口。
    /// </summary>
    /// <param name="serialPortSwitch"></param>
    /// <param name="purpose">用途</param>
    /// <returns></returns>
    public static bool TryOpenViaPurpose(this ISerialPortsSwitch serialPortSwitch, string purpose)
    {
        var (ok, _) = serialPortSwitch.OpenViaPurpose(purpose);
        return ok;
    }
}
